const colors = [
    "#eec996", "#8fb7d3", "#b7d4c6", "#c3bedd", "#f1d5e4",
    "#cae1d3", "#f3c89d", "#d0b0c3", "#819d53", "#c99294",
    "#cec884", "#ff8e70", "#e0a111", "#fffdf6", "#cbd7ac",
    "#e8c6c0", "#dc9898", "#ecc8ba"
];

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let count = 1;
let ww = window.innerWidth;
let wh = window.innerHeight;
const NUM_HEARTS = 50;
let hearts = [];

function init() {
    canvas.width = ww;
    canvas.height = wh;
    hearts = []; // Clear previous hearts
    for (let i = 0; i < NUM_HEARTS; i++) {
        hearts.push(new Heart());
    }
    requestAnimationFrame(render);
}


function Heart() {
    this.x = Math.random() * ww;
    this.y = Math.random() * wh;
    this.opacity = Math.random() * 0.5 + 0.5;
    this.vel = {
        x: (Math.random() - 0.5) * 3, // 稍微减小速度
        y: (Math.random() - 0.5) * 3, // 稍微减小速度
    };
    this.targetScale = Math.random() * 0.1 + 0.02;
    this.scale = this.targetScale * Math.random();
    this.width = 500; // 增加基础宽度
    this.height = 360; // 增加基础高度
}




Heart.prototype.update = function () {
    this.x += this.vel.x;
    this.y += this.vel.y;
    this.scale += (this.targetScale - this.scale) * 0.01;

    if (this.x - this.width > ww || this.x + this.width < 0 ||
        this.y - this.height > wh || this.y + this.height < 0) {
        this.reset();
    }
};

Heart.prototype.reset = function () {
    this.scale = 0;
    this.x = Math.random() * ww;
    this.y = Math.random() * wh;
};

Heart.prototype.draw = function (i) {
    ctx.globalAlpha = this.opacity;
    ctx.font = `${180 * this.scale}px "微软雅黑"`;
    ctx.fillStyle = colors[i % 18];

    // Combine similar text drawing logic if possible
    ctx.fillText("平安喜乐", this.x - this.width * 0.5, this.y - this.height * 0.3, this.width, this.height);
    ctx.fillText("LZ", this.x - this.width * 0.2, this.y + this.height * 0.5, this.width, this.height);
};

function render() {
    ctx.clearRect(0, 0, ww, wh);
    for (let i = 0; i < NUM_HEARTS; i++) {
        hearts[i].update();
        hearts[i].draw(i);
    }
    requestAnimationFrame(render);
}

window.addEventListener("resize", function () {
    ww = window.innerWidth;
    wh = window.innerHeight;
    init(); // Re-initialize on resize
});

init();